package org.tgcloud.group.web.controller.manager;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.*;
import org.tgcloud.group.core.APPResultMessage;
import org.tgcloud.group.core.StringUtil;
import org.tgcloud.group.core.custom.Permissions;
import org.tgcloud.group.core.session.Session;
import org.tgcloud.group.entity.MjBusinessAccount;
import org.tgcloud.group.entity.MjBusinessAccountFollow;
import org.tgcloud.group.service.manager.BusinessAccountManageService;

import java.util.Date;

/**
 * @author: zhy
 * @create: 2023-12-21 13:35
 * @description: 商务客户管理
 */
@Api(tags = "商务客户管理相关接口")
@RestController
@RequestMapping("/manager/pc/user")
@EnableAutoConfiguration
public class BusinessAccountManageController extends ManageAPPBaseController{

    @Autowired
    private BusinessAccountManageService businessAccountManageService;


    @Permissions("businessAccount:view,project:view")
    @ApiOperation(value = "商务客户列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", required = true, dataType = "string")
    })
    @ResponseBody
    @RequestMapping(value = "/getBusinessAccountList", method = RequestMethod.POST)
    public APPResultMessage getBusinessAccountList(@ModelAttribute("session") Session session,
                                                    @RequestParam(required = false) Integer pageNum,
                                                    @RequestParam(required = false) Integer pageSize,
                                                    @RequestParam(required = false) String keyword,
                                                    @RequestParam(required = false) Long startTime,
                                                    @RequestParam(required = false) Long endTime,
                                                   @RequestParam(required = false) Long lastFollowStartTime,
                                                   @RequestParam(required = false) Long lastFollowEndTime,
                                                    @RequestParam(required = false) Integer businessId,
                                                    @RequestParam(required = false) Integer customerStatus,
                                                   @RequestParam(required = false) Integer customerStage,
                                                   @RequestParam(required = false) Integer customerType,
                                                   @RequestParam(required = false) Integer customerProductType,
                                                   @RequestParam(required = false) Integer customerLevel,
                                                   @RequestParam(required = false) Integer customerSource,
                                                   @RequestParam(required = false) Integer departId,
                                                   @RequestParam(required = false) Integer areaId,
                                                   @RequestParam(required = false) Short businessType,
                                                   @RequestParam(required = false) Long followStartTime,
                                                   @RequestParam(required = false) Long followEndTime,
                                                   @RequestParam(required = false) Long checkStartTime,
                                                   @RequestParam(required = false) Long checkEndTime,
                                                   @RequestParam(required = false) Boolean hasCheck,
                                                   @RequestParam(required = false) Boolean hasProtect,
                                                   @RequestParam(required = false) Boolean hasNoProtect,
                                                   @RequestParam(required = false) Boolean hasCooperate,
                                                   @RequestParam(required = false) Boolean hasConnect,
                                                   @RequestParam(required = false) String province,
                                                   @RequestParam(required = false) String city,
                                                   @RequestParam(required = false) Boolean hasOnlyBusiness,
                                                   @RequestParam(required = false) Long protectStartTime,
                                                   @RequestParam(required = false) Long protectEndTime,
                                                   @RequestParam(required = false) String protectType,
                                                   @RequestParam(required = false) String accurateSearchKeyword){
        return businessAccountManageService.getBusinessAccountList(pageNum, pageSize, keyword,
                startTime == null ? null : new Date(startTime), endTime == null ? null : new Date(endTime),
                businessId,customerStatus, customerStage, customerType, customerProductType, customerLevel, customerSource,
                departId, lastFollowStartTime == null ? null : new Date(lastFollowStartTime),
                lastFollowEndTime == null ? null : new Date(lastFollowEndTime), session.getUserType(),
                session.getUserId(), areaId, businessType,
                followStartTime == null ? null : new Date(followStartTime), followEndTime == null ? null : new Date(followEndTime),
                checkStartTime == null ? null : new Date(checkStartTime), checkEndTime == null ? null : new Date(checkEndTime),
                protectStartTime == null ? null : new Date(protectStartTime), protectEndTime == null ? null : new Date(protectEndTime),
                hasCheck, province, city, hasProtect, hasNoProtect, hasCooperate, hasConnect, hasOnlyBusiness,
                protectType, session.getComId(), accurateSearchKeyword);
    }


    @Permissions("businessAccount:add")
    @ApiOperation(value = "新增商务客户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", required = true, dataType = "string")
    })
    @ResponseBody
    @RequestMapping(value = "/doAddBusinessAccount", method = RequestMethod.POST)
    public APPResultMessage doAddBusinessAccount(@ModelAttribute("session") Session session,
                                                 @RequestBody MjBusinessAccount businessAccount){
        if (StringUtil.isBlank(businessAccount.getBrandsName())){
            return new APPResultMessage(6001, "请输入品牌名称");
        }
        return businessAccountManageService.doAddBusinessAccount(session.getUserId(), businessAccount, session.getComId());
    }


    @ApiOperation(value = "查询商务客户是否处于受保")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", required = true, dataType = "string")
    })
    @ResponseBody
    @RequestMapping(value = "/doGetBusinessAccountHas", method = RequestMethod.POST)
    public APPResultMessage doGetBusinessAccountHas(@ModelAttribute("session") Session session,
                                                    @RequestParam String brandsName,
                                                    @RequestParam(required = false) Integer businessAccountId){
        if (StringUtil.isBlank(brandsName)){
            return new APPResultMessage(6001, "请输入品牌名称");
        }
        return businessAccountManageService.doGetBusinessAccountHas(brandsName, businessAccountId);
    }


    @Permissions("businessAccount:edit")
    @ApiOperation(value = "编辑商务客户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", required = true, dataType = "string")
    })
    @ResponseBody
    @RequestMapping(value = "/doEditBusinessAccount", method = RequestMethod.POST)
    public APPResultMessage doEditBusinessAccount(@ModelAttribute("session") Session session,
                                                 @RequestBody MjBusinessAccount businessAccount){
        if (businessAccount.getId() == null){
            return APPResultMessage.MISS_PARAMETER();
        }
        return businessAccountManageService.doEditBusinessAccount(session.getUserId(), businessAccount, session.getComId());
    }


    @Permissions("businessAccount:del")
    @ApiOperation(value = "删除商务客户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", required = true, dataType = "string")
    })
    @ResponseBody
    @RequestMapping(value = "/doDelBusinessAccount", method = RequestMethod.POST)
    public APPResultMessage doDelBusinessAccount(@ModelAttribute("session") Session session,
                                                  @RequestParam Integer businessAccountId){
        return businessAccountManageService.doDelBusinessAccount(businessAccountId,session.getUserId());
    }


    @ApiOperation(value = "商务客户详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", required = true, dataType = "string")
    })
    @ResponseBody
    @RequestMapping(value = "/getBusinessAccountMsg", method = RequestMethod.POST)
    public APPResultMessage getBusinessAccountMsg(@ModelAttribute("session") Session session,
                                                 @RequestParam Integer businessAccountId){
        return businessAccountManageService.getBusinessAccountMsg(businessAccountId);
    }



    @Permissions("businessAccount:viewFollow")
    @ApiOperation(value = "查看跟进记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", required = true, dataType = "string")
    })
    @ResponseBody
    @RequestMapping(value = "/viewFollowBusinessList", method = RequestMethod.POST)
    public APPResultMessage viewFollowBusinessList(@ModelAttribute("session") Session session,
                                                 @RequestParam Integer businessAccountId,
                                                   @RequestParam(required = false) Long startTime,
                                                   @RequestParam(required = false) Long endTime,
                                                   @RequestParam(required = false) Integer pageNum,
                                                   @RequestParam(required = false) Integer pageSize,
                                                   @RequestParam(required = false) Integer followType){
        return businessAccountManageService.viewFollowBusinessList(businessAccountId, pageNum, pageSize,
                startTime == null ? null : new Date(startTime), endTime == null ? null : new Date(endTime),
                followType);
    }


    @Permissions("businessAccount:addFollow")
    @ApiOperation(value = "新增跟进记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", required = true, dataType = "string")
    })
    @ResponseBody
    @RequestMapping(value = "/doAddFollowBusinessList", method = RequestMethod.POST)
    public APPResultMessage doAddFollowBusinessList(@ModelAttribute("session") Session session,
                                                    @RequestBody MjBusinessAccountFollow businessAccountFollow){
        return businessAccountManageService.doAddFollowBusinessList(businessAccountFollow, session.getUserId(), session.getComId());
    }

    @Permissions("businessAccount:editFollow")
    @ApiOperation(value = "编辑跟进记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", required = true, dataType = "string")
    })
    @ResponseBody
    @RequestMapping(value = "/doEditFollowBusinessList", method = RequestMethod.POST)
    public APPResultMessage doEditFollowBusinessList(@ModelAttribute("session") Session session,
                                                    @RequestBody MjBusinessAccountFollow businessAccountFollow){
        return businessAccountManageService.doEditFollowBusinessList(businessAccountFollow, session.getUserId(), session.getComId());
    }

    @Permissions("businessAccount:delFollow")
    @ApiOperation(value = "删除跟进记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", required = true, dataType = "string")
    })
    @ResponseBody
    @RequestMapping(value = "/doDelFollowBusinessList", method = RequestMethod.POST)
    public APPResultMessage doDelFollowBusinessList(@ModelAttribute("session") Session session,
                                                    @RequestParam int id){
        return businessAccountManageService.doDelFollowBusinessList(id);
    }

    @Permissions("businessAccount:updateBusinessUser")
    @ApiOperation(value = "修改品牌商务")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", required = true, dataType = "string"),
            @ApiImplicitParam(name = "updateType", value = "1、商务，2、品牌", required = true, dataType = "string"),
            @ApiImplicitParam(name = "updateIds", value = "修改所选择的id集合", required = true, dataType = "string"),
            @ApiImplicitParam(name = "businessUserId", value = "商务id", required = true, dataType = "string")
    })
    @ResponseBody
    @RequestMapping(value = "/doUpdateBusinessUser", method = RequestMethod.POST)
    public APPResultMessage doUpdateBusinessUser(@ModelAttribute("session") Session session,
                                                 @RequestParam Integer updateType, // 1、商务，2、品牌
                                                 @RequestParam String updateIds,
                                                 @RequestParam Integer businessUserId){
        if (StringUtil.isBlank(updateIds) || updateType == null || businessUserId == null){
            return APPResultMessage.MISS_PARAMETER();
        }
        return businessAccountManageService.doUpdateBusinessUser(updateType, updateIds, businessUserId);
    }



    @ApiOperation(value = "查询历史商务与客户关联时间")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", required = true, dataType = "string")
    })
    @ResponseBody
    @RequestMapping(value = "/getHistoryBusinessAndAccountTimeList", method = RequestMethod.POST)
    public APPResultMessage getHistoryBusinessAndAccountTimeList(@ModelAttribute("session") Session session,
                                                                 @RequestParam(required = false) Integer businessId,
                                                                 @RequestParam(required = false) Integer businessAccountId){
        return businessAccountManageService.getHistoryBusinessAndAccountTimeList(businessId, businessAccountId);
    }


    @Permissions("businessAccount:updateInsured")
    @ApiOperation(value = "修改品牌商务受保状态，因没有跟进而掉保")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", required = true, dataType = "string"),
    })
    @ResponseBody
    @RequestMapping(value = "/doUpdateBusinessInsured", method = RequestMethod.POST)
    public APPResultMessage doUpdateBusinessInsured(@ModelAttribute("session") Session session,
                                                 @RequestParam Integer businessUserId){
        return businessAccountManageService.doUpdateBusinessInsured(businessUserId);
    }

}
